package com.yitong.profile.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.yitong.base.controller.BaseController;
import com.yitong.base.dao.api.Login;
import com.yitong.base.domain.Message;
import com.yitong.base.util.Constant;
import com.yitong.base.util.HttpUtil;
import com.yitong.base.util.JsonUtils;
import com.yitong.base.util.StringUtil;
import com.yitong.profile.domain.TabOrg;
import com.yitong.profile.domain.TabRight;
import com.yitong.profile.domain.TabUser;

@Controller
public class LoginController extends BaseController{

	@Inject
	public Login login;
	
	@RequestMapping(value="userLogin", method=RequestMethod.POST)
	@SuppressWarnings({"rawtypes", "unchecked" })
	public String userLogin(){
		System.out.println("开始");
		Map result=new HashMap();
		String target="login";
		try {
			String userName=request.getParameter("userName");
			String password=request.getParameter("password");
			if(!StringUtil.isNullOrEmpty(userName) && !StringUtil.isNullOrEmpty(password)){
				result.put("userName", userName);
				String url=Constant.AUTH_URL+Constant.USERLOGIN_ACTION+"userName="+userName+Constant.URL_SPLIT+"password="+password;
				String userContent = HttpUtil.doGet(url, request);
				if(userContent != null){
					Message userMessage=login.jsonStrToMessageTabUser(userContent);
					if(userMessage.getCode()!=0){
						TabUser user=(TabUser)userMessage.getInfo();
						this.session.setAttribute(Constant.SESSION_LOGIN_USER, user);
						String t=userMessage.getMessage();
						System.out.println("------"+t);
						TabOrg org=(TabOrg) JsonUtils.jsonToBean(t, TabOrg.class);
						this.session.setAttribute(Constant.SESSION_LOGIN_USER_ORG, org);
						request.setAttribute("userName", user.getUserName());
						//登录成功后获取登录用户权限
						String authUrl=Constant.AUTH_URL+Constant.USER_RRIGHT_TOP_ACTION+"userId="+user.getUserId();
						String userAuthContent= HttpUtil.doGet(authUrl, request);
						Message userAuthMessage=login.jsonStrToMessageTabRight(userAuthContent);
						//判断权限
						if(userAuthMessage!=null&&userAuthMessage.getCode()!=0){
							List<TabRight> rightList=(List<TabRight>) userAuthMessage.getInfo();
							result.put("rightList", rightList);
							if(rightList!=null&&rightList.size()>0){
								TabRight rightMap= rightList.get(0);
								String authSubUrl=Constant.AUTH_URL+Constant.RIGHT_SUB_ACTION+"rightId="+rightMap.getRightId()+Constant.URL_SPLIT+"userId="+loginUser().getUserId();
								String authSubContent=HttpUtil.doGet(authSubUrl, request);
								if(authSubContent!=null){
									Message userAuthSubMessage=login.jsonStrToMessageTabRight(authSubContent);
									if(userAuthSubMessage!=null&&userAuthSubMessage.getCode()!=0){
										List<TabRight> righSubtList=(List<TabRight>) userAuthSubMessage.getInfo();
										result.put("righSubtList", righSubtList);
										result.put("rightSubTitle", rightList.get(0));
										//登录成功
										target="menu/index";
									}else{
										target="login";
										result.put("message", userAuthSubMessage.getMessage());
									}
								}else{
									target="login";
									result.put("message", "未获得登录权限");
								}
							}else{
								target="login";
								result.put("message", userAuthMessage.getMessage());
							}
						}else{
							//登录失败
							target="login";
							result.put("message", "用户名或密码错误");
						}
					}else{
						target="login";
						result.put("message", "后台错误，请重新尝试 ");
					}
				}else{
					target="login";
					result.put("message", "未从权限平台获取到用户信息");
				}
			}else{
				target="login";
				result.put("message", "用户名或密码不能为空");
			}
		} catch (Exception e) {
			e.printStackTrace();
			result.put("message", "登录异常");
			target="login";
		}
		this.setParameters(result);
		return target;
	}
	
	
	/**
	 * 退出登录，强制清空session
	 * @return String
	 */
	@RequestMapping(value="loginOut")
	public String loginExit(){
		if(null != this.session){
			this.session.invalidate();
			this.session = null;
		}
		return "login";
	}
	
	
	public TabUser loginUser(){
		TabUser loginUser=(TabUser) this.session.getAttribute(Constant.SESSION_LOGIN_USER);
		return loginUser;
	}
}
